package com.ndp.fb.mq.service.schedulerservice.adpublish;

import com.ndp.fb.enums.publish.PublishResultType;
import com.ndp.fb.rdb.api.PublishResultDetailRdbService;
import com.ndp.fb.rdb.api.PublishResultRecordRdbService;
import com.ndp.fb.rdb.model.BaseAdLevel;
import com.ndp.fb.rdb.model.PublishBatch;
import com.ndp.fb.rdb.model.PublishResultDetail;
import com.ndp.fb.rdb.model.PublishResultRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 广告发布流程抽象类
 * 负责在 adcreative ，campaign，adset，adgrop 中的一些通用方法 和  publish ，callback 的约束定义
 * Created by hunk.qin on 2016/2/4.
 */
public abstract class PublishBase {

    private  Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 发布方法，去Facebook上去创建不同level 的对象
     * @param batchId  批次ID
     * @param nextLevelIndexs 下一级别的Level indexs 索引
     * @param preLevel 上一级别的Level对象，adset 的上级别是 campaign
     */
    abstract void publish(Long batchId, String nextLevelIndexs, BaseAdLevel preLevel);

    /**
     * 处理facebook的调用返回
     * @param equipment
     * @param fbObj
     */
    abstract void callback(Map<String, String> equipment, Object fbObj);

    abstract PublishResultRecordRdbService getPublishResultRecordRdbService();

    abstract  PublishResultDetailRdbService getPublishResultDetailRdbService();




    public boolean checkBatch(Long batchId,PublishBatch publishBatch) {
        if (publishBatch == null) {
            logger.info("publishBatch is null!id:"+batchId);
            return true;
        }
        if (publishBatch.getUserId() == null) {
            logger.info("fbUser is null!fbUser" + publishBatch.getUserId());
            return true;
        }
        return false;
    }

    public void savePublishResult(BaseAdLevel baseAdLevel, PublishResultType publishResultType, String exception,String type) {
        //1、构造 PublishResultRecord 存储publish_result_record
        try {
            PublishResultRecord publishResultRecord = new PublishResultRecord();
            publishResultRecord.setPublishResultId(baseAdLevel.getPublishResultId());
            publishResultRecord.setType(publishResultType.getIndex());
            Long publishResultRecordId = getPublishResultRecordRdbService().save(publishResultRecord);
            //2、存储 publish_result_detail
            PublishResultDetail publishResultDetail = new PublishResultDetail();
            publishResultDetail.setPublishResultRecordId(publishResultRecordId);
            publishResultDetail.setResult(publishResultType.name());

            publishResultDetail.setParam(baseAdLevel.toString());
            if(exception != null && exception.length() > 3900){
                exception = exception.substring(0,3900);
            }
            publishResultDetail.setException(exception);

            publishResultDetail.setObjectType(type);
            publishResultDetail.setType(publishResultType.name());
            getPublishResultDetailRdbService().save(publishResultDetail);
        }catch (Exception e){
            logger.error("save publish to db error "+e.getMessage(),e);
        }
    }



}
